package dfl.bysj.handler;

import dfl.bysj.controller.push.SendMessage;
import dfl.bysj.entity.StudentSeatRelation;
import dfl.bysj.service.StudentSeatRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.List;

/**
 * @Author: DFL
 * @Data: 2022/5/710:27
 */
@Slf4j
@Component
public class ReserveCheckTask {

    private StudentSeatRelationService studentSeatRelationService;

    private SendMessage sendMessage;


    @Autowired
    public void setSendMessage(SendMessage sendMessage) {
        this.sendMessage = sendMessage;
    }

    @Autowired
    public void setStudentSeatRelationService(StudentSeatRelationService studentSeatRelationService) {
        this.studentSeatRelationService = studentSeatRelationService;
    }

    /**
     * 定时检查预约开始时间
     * 1、如果现在的时间有超过预约时间的就返回错误信息
     * 1、开始的时间应涵盖
     */
//    @Scheduled(cron = "0 0/10 7-22 * * ? ")
    @Scheduled(cron = "0 0/1 * * * ?  ")
    public void reserveCheck(){
        log.info("正在执行定时任务中。。。。。。");
        //获取预约表不为空且overtimeCheck=false且时间为今天
       List<StudentSeatRelation> studentSeatRelations= studentSeatRelationService.getRelationTodayAndOverTimeCheck();

        if (!studentSeatRelations.isEmpty()){
            //获取当前时间，判断当前时间
            //签到超时
            LocalDateTime now=LocalDateTime.now();
            studentSeatRelations.stream()
                    .filter(s->s.getResStartTime().isBefore(now))
                    .filter(s-> !s.getSignState())
                    .forEach(s->{
                        log.info("签到超时。。。。。。。");
                        //设置过期,发布提醒
                        sendMessage.pushSignLost(s,studentSeatRelationService.signLost(s));

                    });

            //设定签到提醒
            //计算时间差值
            studentSeatRelations.stream()
                    .filter(s->s.getResStartTime().isAfter(now))
                    .filter(s->!s.getSignState())
                    .forEach(s->{
                        Duration duration=Duration.between(now, s.getResStartTime());
                        if (duration.toHours()==0 && duration.toMinutes()<30){
                            log.info("签到提醒。。。。。。。");
                            //发送签到
                            sendMessage.pushSign(s,"距离签到结束时间还剩："+duration.toMinutes());
                        }

                    });

            //签退超时
            studentSeatRelations.stream()
                    .filter(s->s.getResEndTime().isBefore(now))
                    .filter(s-> !s.getSingBackState())
                    .forEach(s->{
                        log.info("签退超时。。。。。。。");
                        //设置过期,发布提醒
                        sendMessage.pushSignLost(s,studentSeatRelationService.signBackLost(s));

                    });
            //设定签退提醒
            //计算时间差值
            studentSeatRelations.stream()
                    .filter(s->s.getResEndTime().isAfter(now))
                    .filter(s->!s.getSignState())
                    .forEach(s->{
                        Duration duration=Duration.between(now, s.getResEndTime());
                        if (duration.toHours()==0 && duration.toMinutes()<30){
                            log.info("签退提醒。。。。。。。");
                            //发送签到
                            sendMessage.pushSign(s,"距离签退结束时间还剩："+duration.toMinutes());
                        }

                    });


        }
    }

    /**
     * 定时任务：
     * 检查正在处罚中的用户
     * 如果时间已满，修改记录数
     * 每天执行一次
     */


}
